﻿using System;
using System.Collections.Generic;
using Argotic.Syndication;
using MIXer.Core.Domain;
using System.Threading.Tasks;
using System.ServiceModel.Syndication;
using System.Xml;
using System.Linq;
using Argotic.Extensions.Core;

namespace MIXer.Ch9
{
    public class Ch9RssFeed
    {
        public async static Task<IList<Session>> GetFilesFromFeed(Uri feed, string eventCode)
        {
            return await Task.Factory.StartNew<IList<Session>>(() => 
            {
                var rssFeed = RssFeed.Create(feed);
                var files = new List<Session>();

                foreach (var item in rssFeed.Channel.Items)
                {
                    string sessionCode = item.Guid.Value.Substring(item.Guid.Value.LastIndexOf("/") + 1);
                    string author = String.Empty;

                    if (item.Extensions != null)
                    {
                        var extension = item.FindExtension<DublinCoreElementSetSyndicationExtension>();
                        if (extension != null) author = extension.Context.Creator;
                    }

                    if (item.Enclosures != null)
                    {
                        foreach (var enclosure in item.Enclosures)
                        {
                            files.Add(new Session
                            {
                                Abstract = item.Description,
                                Title = item.Title,
                                VideoUrl = enclosure.Url,
                                EventCode = eventCode,
                                SessionCode = sessionCode,
                                Length = enclosure.Length,
                                Author = author
                            });
                        }
                    }
                }

                return files;
            });
        }
        public async static Task<IList<Session>> GetFilesFromFeed2(Uri feed, string eventCode)
        {
            return await Task.Factory.StartNew<IList<Session>>(() =>
            {
                var files = new List<Session>();

                using(XmlReader reader = XmlReader.Create(feed.AbsoluteUri))
                {
                    var rssFeed = SyndicationFeed.Load(reader);

                    foreach (var item in rssFeed.Items)
                    {
                        string sessionCode = item.Id.Substring(item.Id.LastIndexOf("/") + 1);

                        var session = new Session 
                        {
                            Abstract = item.Summary.Text,
                            Title = item.Title.Text,
                            EventCode = eventCode,
                            SessionCode = sessionCode
                        };

                        if (item.Links != null)
                        {
                            var video = (from link in item.Links
                                         where link.RelationshipType == "enclosure"
                                         select link).FirstOrDefault();

                            if (video != null)
                            {
                                session.VideoUrl = video.Uri;
                                session.Length = video.Length;
                            }
                        }

                        files.Add(session);
                    }
                }

                return files;
            });
        }
    }
}
